package com.songtech.uitest.module.order;

import com.songtech.uitest.entities.Indent;
import com.songtech.uitest.module.common.AlertHandler;
import com.songtech.uitest.module.common.MenuHandler;
import com.songtech.uitest.module.common.TabHandler;
import com.songtech.uitest.module.screen.ScreenTower;
import com.songtech.uitest.resources.IndentResource;
import com.songtech.uitest.resources.ThirdResource;
import com.songtech.uitest.util.Space;
import com.songtech.uitest.util.StepParser;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component
public class OrderTower {

    @Autowired
    @Qualifier("indentResource")
    private IndentResource indentResource;

    @Autowired
    @Qualifier("indent")
    private Indent indent;

    @Autowired
    private ThirdResource thirdResource;

    @Autowired
    private TabHandler tabHandler;

    @Autowired
    private MenuHandler menuHandler;

    @Autowired
    private AlertHandler alertHandler;

    @Autowired
    private StepParser stepParser;

    private Logger logger = LoggerFactory.getLogger(OrderTower.class);

    //打印日志前的缩进空格
    @Autowired
    private Space space;


    public void orderTower(String step) throws Exception {

        WebDriver driver = stepParser.parseDriver(step);

        tabHandler.closeAllTabs(driver);
        //打开模拟页
        menuHandler.clickMenuItem(driver, "接口模拟", "订单确认数据模拟", "");
        driver.switchTo().frame("iframeundefined");
        Thread.sleep(6000);

        logger.info(space.getSpace() + "开始填写订单详情表单");

        //填表单
        driver.findElement(By.id("mobileOrderCode")).sendKeys(indent.getMobileId());
        driver.findElement(By.id("towerOrderCode")).sendKeys(indent.getTowerId());
        Select priority = new Select(driver.findElement(By.id("priority")));
        priority.selectByVisibleText(indentResource.getOrderPriority());
        driver.findElement(By.id("province")).clear();
        driver.findElement(By.id("province")).sendKeys(indentResource.getOrderProvince());
        driver.findElement(By.id("region")).clear();
        driver.findElement(By.id("region")).sendKeys(indentResource.getOrderRegion());
        driver.findElement(By.id("county")).clear();
        driver.findElement(By.id("county")).sendKeys(indentResource.getOrderCountry());
        driver.findElement(By.id("siteName")).clear();
        driver.findElement(By.id("siteName")).sendKeys(indentResource.getOrderSiteName());
        driver.findElement(By.id("longitude")).clear();
        driver.findElement(By.id("longitude")).sendKeys(indentResource.getOrderLongitude());
        driver.findElement(By.id("latitude")).clear();
        driver.findElement(By.id("latitude")).sendKeys(indentResource.getOrderLatitude());
        //是否共享
        Select isSharing = new Select(driver.findElement(By.id("isSharing")));
        isSharing.selectByVisibleText(indentResource.getOrderIsSharing());
        driver.findElement(By.id("formerCustomerCount")).sendKeys(indentResource.getOrderFormerCustomerCount());
        driver.findElement(By.id("increaseCustomerCount")).sendKeys(indentResource.getOrderIncreaseCustomerCount());
        //建设方式
        Select buildType = new Select(driver.findElement(By.id("buildType")));
        buildType.selectByVisibleText(indentResource.getOrderBuildType());
        ((JavascriptExecutor) driver).executeScript("window.scrollTo(0, document.body.scrollHeight)");
        Thread.sleep(1000);
        //请求时间
        driver.findElement(By.id("datetimepicker3")).findElement(By.cssSelector("span.glyphicon.glyphicon-calendar")).click();
        Thread.sleep(1000);
        //移动是否使用站址
        Select existCm = new Select(driver.findElement(By.id("existCm")));
        existCm.selectByVisibleText(indentResource.getOrderExistCm());
        //站址涉及客户数
        driver.findElement(By.id("siteclientCount")).clear();
        driver.findElement(By.id("siteclientCount")).sendKeys(indentResource.getOrderSiteClientCount());

        //应交付时间
        driver.findElement(By.id("datetimepicker4")).findElement(By.cssSelector("span.glyphicon.glyphicon-calendar")).click();
        Thread.sleep(1000);


        driver.findElement(By.id("saveSet")).click();


        //完成后，设置当前站址类型
        indent.setCurrentBuildType(indentResource.getOrderResourceType());
        //记录单子的流程进行位置
        indent.setStepPosition(indent.getStepPosition() + 1);

        logger.info(space.getSpace() + "模拟订单表单提交完成，当前站址类型为" + indent.getCurrentBuildType());

        Thread.sleep(6000);

//        AlertHandler.checkAndGo(driver);  //可能是页面长了，有问题

        Thread.sleep(3000);


        tabHandler.closeAllTabs(driver);


    }



    public void order3Tower(String step) throws Exception {

        WebDriver driver = stepParser.parseDriver(step);


        tabHandler.closeAllTabs(driver);
        //打开模拟页
        menuHandler.clickMenuItem(driver, "铁塔建设管理", "订单编制", "");
        driver.switchTo().frame("iframeundefined");
        Thread.sleep(6000);


        //查询此单
        driver.findElement(By.id("keywords")).sendKeys(indent.getMobileId());
        driver.findElement(By.xpath("//button[text()='查询']")).click();
        Thread.sleep(6000);

        //拖动滚动条到最右方
        String js = "$(\"div.fixed-table-body\").scrollLeft(document.body.scrollWidth)";
        ((JavascriptExecutor) driver).executeScript(js);
        Thread.sleep(1000);
        driver.findElement(By.xpath("//button[text()='编制订单']")).click();

        //转到三方订单编制详细页
        Thread.sleep(5000);
        //关闭上一个标签页
        tabHandler.closeFirstTab(driver);
        //回到三方订单编制详细页上
        WebElement frame = driver.findElements(By.name("iframe0")).get(1);
        driver.switchTo().frame(frame);

        Thread.sleep(1000);
        logger.info(space.getSpace() + "开始填写三方订单编制表单");
        //填写表单


        //重置
        driver.findElement(By.xpath("//button[text()='置空']")).click();
        Thread.sleep(2000);
        driver.findElement(By.xpath("//button[text()='确定']")).click();
        Thread.sleep(1000);


        //预期交付时间
        driver.findElement(By.id("expectedDeliveryTime")).click();
        //站址编码，随便写
        WebElement siteAddressCode = driver.findElement(By.id("siteAddressCode"));
        siteAddressCode.clear();
        siteAddressCode.sendKeys("aabbcc");
        //三方站址名称
        WebElement siteAddressName = driver.findElement(By.id("siteAddressName"));
        siteAddressName.clear();
        siteAddressName.sendKeys(thirdResource.getOrderSiteAddressName());
        //建设方式：存量直接满足，新建，改造
        Select buildType = new Select(driver.findElement(By.id("buildType")));
        buildType.selectByVisibleText(thirdResource.getOrderBuildType());
        driver.findElement(By.id("longitude")).clear();
        driver.findElement(By.id("longitude")).sendKeys(thirdResource.getOrderLongitude());
        driver.findElement(By.id("latitude")).clear();
        driver.findElement(By.id("latitude")).sendKeys(thirdResource.getOrderLatitude());

        driver.findElement(By.id("address")).sendKeys("三方订单编制所写详细地址");
        driver.findElement(By.id("antennaHeightDuration")).sendKeys("15~20");
        driver.findElement(By.id("antennaCount")).sendKeys("3");
        //移动是否已使用站址
        Select existCm = new Select(driver.findElement(By.id("existCm")));
        existCm.selectByVisibleText(thirdResource.getOrderExistCm()); //是，否
        //站址涉及客户数,1,2,3
        Select siteClientCount = new Select(driver.findElement(By.id("siteclientCount")));
        siteClientCount.selectByVisibleText(thirdResource.getOrderSiteClientCount());

        //应交付时间
        driver.findElement(By.id("deliveryTime")).click();

        //原有客户数
        driver.findElement(By.id("formerCustomerCount")).sendKeys(thirdResource.getOrderFormerCustomerCount());
        //新增客户数
        driver.findElement(By.id("increaseCustomerCount")).sendKeys(thirdResource.getOrderIncreaseCustomerCount());

        driver.findElement(By.id("bbuCount")).sendKeys("3");
        driver.findElement(By.id("rruCount")).sendKeys("3");

        //预估产品租赁费
        driver.findElement(By.id("pojectFee")).sendKeys("1111");
        //预估场地费
        driver.findElement(By.id("rentFee")).sendKeys("2222");
        //预估电力引入费
        driver.findElement(By.id("powerGenFee")).sendKeys("3333");


        driver.findElement(By.xpath("//button[text()='确定并提交']")).click();

        Thread.sleep(5000);

        alertHandler.checkAndGo(driver);

        //完成后，设置当前站址类型
        indent.setCurrentBuildType(thirdResource.getOrderResourceType());
        //记录单子的流程进行位置
        indent.setStepPosition(indent.getStepPosition() + 1);

        logger.info(space.getSpace() + "三方订单编制表单提交完成，当前站址类型为" + indent.getCurrentBuildType());

        Thread.sleep(4000);

        tabHandler.closeAllTabs(driver);


    }

}
